import {contextBridge, ipcRenderer} from 'electron'
import * as fs from "fs";
import {DownloadFileProgress} from "@/utils/FileUtil";


contextBridge.exposeInMainWorld('electronAPI', {
    closeApp: () => ipcRenderer.send('close-app'),
    maxApp: () => ipcRenderer.send('max-app'),
    unmaxApp: () => ipcRenderer.send('unmax-app'),
    minApp: () => ipcRenderer.send('min-app'),
    maxHandle: (isMax: boolean) => ipcRenderer.on('max-handle', isMax as never),
    windowToControlWarn: (url: string) => {
        ipcRenderer.send('openBrowser', url);
    },
    chooseFolder: () => {
        return ipcRenderer.invoke('openDialog', {
            properties: ['openDirectory']
        });
    },
    download: (url: string, filename: string) => {
        return ipcRenderer.send('download', url, filename);
    },
    unzip: (path: fs.PathLike) => {
        return ipcRenderer.invoke('unzip', path);
    },
    onReleaseDownload: (cb: (num: DownloadFileProgress) => void) => {
        ipcRenderer.on('onReleaseDownload', (event, progress: DownloadFileProgress) => {
            cb(progress);
        });
    },
    closeReleaseDownload: () => {
        ipcRenderer.send('closeReleaseDownload');
    },
    onReleaseDownloadError: (cb: () => void) => {
        ipcRenderer.on('onReleaseDownloadError', (event) => {
            cb();
        });
    },
    onReleaseDownloadSuccess: (cb: () => void) => {
        ipcRenderer.on('onReleaseDownloadSuccess', (event) => {
            cb();
        });
    },
})
